<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="90px"
    >
      <el-form-item label="关键字" prop="searchValue">
        <el-input
          v-model="queryParams.searchValue"
          placeholder="请输入关键字"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="入库状态" prop="storageStatus">
        <el-select
          v-model="queryParams.storageStatus"
          placeholder="请选择入库状态"
          clearable
          @change="handleQuery"
        >
          <el-option
            v-for="dict in dict.type.storage_status"
            :key="dict.label"
            :label="dict.label"
            :value="dict.label"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="初始化时间" prop="initTime">
        <el-date-picker
          v-model="initTime"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          @change="handleQuery"
        >
        </el-date-picker>
      </el-form-item>
      <el-form-item label="入库时间" prop="storageTime">
        <el-date-picker
          v-model="storageTime"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          @change="handleQuery"
        >
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb-8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['equipment:equipment:add']"
          >设备入库</el-button
        >
        <span style="padding-left: 20px; font-size: 14px; color: #666"
          >设备统计总数：{{ storageTotal }}台； 已入库：{{ storageOnNum }}台；
          待入库：{{ storageNoNum }}台；</span
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="equipmentList"
      @selection-change="handleSelectionChange"
    >
      <!-- <el-table-column type="selection" width="55" align="center" /> -->
      <el-table-column
        label="设备序列号"
        align="center"
        prop="serialNo"
        width="180"
      />
      <el-table-column
        label="设备终端号"
        align="center"
        prop="terminalNo"
        width="100"
      />
      <el-table-column label="型号" align="center" prop="modelNo" width="80" />
      <el-table-column label="有线MAC地址" align="center" prop="wiredMacaddr" width="140" />
      <el-table-column label="无线MAC地址" align="center" prop="wirelessMacaddr" width="140" />
      <el-table-column
        label="入库状态"
        align="center"
        prop="storageStatus"
        width="80"
      />
      <el-table-column
        label="初始化时间"
        align="center"
        prop="initTime"
        width="180"
      >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.initTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="入库时间"
        align="center"
        prop="storageTime"
        width="180"
      >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.storageTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="remark" min-width="200" />
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
        width="120px"
        fixed="right"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['equipment:equipment:edit']"
            >编辑</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.params.pageNum"
      :limit.sync="queryParams.params.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改设备管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" :show-message="false" label-width="100px">
        <el-form-item label="设备序列号" prop="serialNo" required>
          <el-input v-model="form.serialNo" placeholder="请输入设备序列号" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <el-dialog
      :title="title"
      :visible.sync="remarkOpen"
      width="500px"
      append-to-body
    >
      <el-form ref="form" :model="form" :rules="rules" :show-message="false" label-width="100px">
        <el-form-item label="备注" prop="remark" required>
          <el-input
            type="textarea"
            v-model="form.remark"
            placeholder="请输入备注"
            autosize
          />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listEquipment,
  getEquipment,
  delEquipment,
  addEquipment,
  updateEquipment,
  save,
  equipmentStorage,
  storageStatusList,
} from "@/api/equipment/equipment";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";
export default {
  name: "Equipment",
  dicts: ["storage_status"],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 设备管理表格数据
      equipmentList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 是否显示弹出层
      remarkOpen: false,
      // 查询参数
      queryParams: {
        params: { pageNum: 1, pageSize: 10 },
        searchType: 1,
        searchValue: null,
        storageStatus: null,
        initTimeStart: null,
        initTimeEnd: null,
        storageTimeStart: null,
        storageTimeEnd: null,
      },
      initTime: "",
      storageTime: "",
      storageTotal: 0,
      storageNoNum: 0,
      storageOnNum: 0,
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
    };
  },
  created() {
    this.getList();
    this.storageStatusInit();
  },
  activated() {
    this.getList();
    this.storageStatusInit();
  },
  methods: {
    storageStatusInit() {
      storageStatusList().then((response) => {
        let data = response.data;
        let res = data.filter((item) => {
          return item == "1";
        });
        this.storageTotal = data.length;
        this.storageOnNum = res.length;
        this.storageNoNum =
          Number(this.storageTotal) - Number(this.storageOnNum);
      });
    },

    /** 查询设备管理列表 */
    getList() {
      this.loading = true;
      this.queryParams.initTimeStart = this.initTime && this.initTime.length ? this.initTime[0] : "";
      this.queryParams.initTimeEnd = this.initTime && this.initTime.length ? this.initTime[1] : "";
      this.queryParams.storageTimeStart = this.storageTime && this.storageTime.length ? this.storageTime[0] : "";
      this.queryParams.storageTimeEnd = this.storageTime && this.storageTime.length ? this.storageTime[1] : "";
      listEquipment(this.queryParams).then((response) => {
        this.equipmentList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.remarkOpen = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        serialNo: null,
        terminalNo: null,
        modelNo: null,
        storageStatus: "0",
        initTime: null,
        storageTime: null,
        remark: null,
        channelId: null,
        companyId: null,
        isActivated: null,
        channelTime: null,
        activationTime: null,
        activationStatus: "0",
        expirationTime: null,
        renewalCycle: null,
        renewalReminder: null,
        enabledStatus: "0",
        desktopVersion: null,
        appVersion: null,
        appLoginStatus: "0",
        isVideoCall: null,
        updateTime: null,
        setId: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.params.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.queryParams.searchValue = null;
      this.queryParams.storageStatus = null;
      this.queryParams.initTimeStart = null;
      this.queryParams.initTimeEnd = null;
      this.queryParams.storageTimeStart = null;
      this.queryParams.storageTimeEnd = null;
      this.initTime = "";
      this.storageTime = "";
      //this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "设备入库";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      this.form = row;
      this.remarkOpen = true;
      this.title = "编辑";
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            let saveData = {
              id: this.form.setId,
              equipmentId: this.form.id,
              userType: 0,
              remark: this.form.remark,
            };
            save(saveData).then((response) => {
              this.$modal.msgSuccess("编辑成功");
              this.remarkOpen = false;
              this.getList();
            });
          } else {
            equipmentStorage(this.form.serialNo).then((response) => {
              this.$modal.msgSuccess("入库成功");
              this.open = false;
              this.getList();
              this.storageStatusInit();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm('是否确认删除设备管理编号为"' + ids + '"的数据项？')
        .then(function () {
          return delEquipment(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    }
  },
};
</script>
